package themaopdracht;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.appengine.api.datastore.Key;
import java.sql.*;
@PersistenceCapable
public class Gegevens {
   
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
	@Persistent
	private String studentnr;
	@Persistent
	private String naam;
	@Persistent
	private String emailadres;
	@Persistent
	private String afmeldenvan;
	@Persistent
	private String afmeldentot;

	
	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}



	public String getStudentnr()	{
		return studentnr;
	}
	
	public void setStudentnr(String studentnr)	{
		if (studentnr == null) {
			studentnr="";
		}	
		this.studentnr = studentnr;
	}
	
	public boolean studentnrc(){
		if(studentnr.matches("^[0-9]+$")){ 
			return true;
	 }else{	 
		 studentnr = "foutief nr";
		return false;
	 }
		
	}
	
	public String getNaam() {
		return naam;
	}
	
	public void setNaam(String naam) {
		if (naam == null) {
			naam="";
		}
		this.naam = naam;
	}
	
	public boolean naamc(){
		if ( naam != null ){
			return true;
		}else{
			return false;
		}
	}
	
	public String getEmailadres() {
		return emailadres;
	}
	
	public void setEmailadres(String emailadres)	{
		if (emailadres == null) {
			emailadres="";
		}
		
	}
		public boolean emailc(){
		String regex = "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}";
	
		if (emailadres.matches(regex)){
		return true;
		}
		else	{
			emailadres="foutief emailadres";
			return false;
		}
		
	}
	
	
	public String getAfmeldenvan() {
		return afmeldenvan;
	}
	
	public void setAfmeldenvan(String afmeldenvan){}
	
	public boolean vanc(){
		String regex ="(0[1-9]|[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012]|[1-9])-(19|20)\\d{2}"; 
		
	if(afmeldenvan.matches(regex)){
		return true;
	}
	else {
		afmeldenvan="ongeldig datum formaat gebruik dit formaat dd-mm-yyyy";
		return false;
	}
	
	}
	
	public String getAfmeldentot() {
		return afmeldentot;
	}
	
	public void setAfmeldentot (String afmeldentot) {}
	
	public boolean totc(){
		String regex ="^(0[1-9]|[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012]|[1-9])-(19|20)\\d{2}";
	
		if(afmeldentot.matches(regex)){
		return true;
		}
		else {
			afmeldentot="ongeldig datum formaat gebruik dit formaat dd-mm-yyyy";
			return false;
		}
		
	}
	
	public boolean eindcheck() {
	if ( studentnrc() == true && naamc() == true && emailc() == true && vanc()==true && totc() == true ){
		 return true;
	}
	else{ return false;
	}}
		
			
			
		public void connection(){
			
			if (eindcheck() == true){
				//jdbc gedeelte
				try {
				Connection connection = null;
	            Class.forName("com.mysql.jdbc.Driver");
	            connection = DriverManager.getConnection("jdbc:mysql://localhost/student", "username", "password");

	            Statement statement = connection.createStatement();

	            String command = "INSERT INTO studenten "+"VALUES (key, naam,studentnr,emailadres,afmeldenvan,afmeldentot)";
	            statement.executeUpdate(command);
             

	            connection.close(); 
				} catch (Exception e) {
					System.err.println("er is een exception opgetreden! "); 
		            System.err.println(e.getMessage()); 
		        } 
			}
			else{  System.err.println("mislukt ");
			}
			}

	

	
		
	}


